﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BookingTour : System.Web.UI.Page
{
    eStoreDataContext db= new eStoreDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        // Lay Id cua tour
        int TourId = int.Parse(RouteData.Values["TourId"].ToString());
        var tour = db.Tours.Single(p => p.TourId == TourId);
        txtTotal.Text = tour.UnitPrice.ToString(); 
        // Hiển thị thông tin customer
        var user = Session["user"] as Customer;
        if(Session["user"]!=null)
        {
            customer_booking.Visible = false;
            txtUsername.Text = user.Fullname;
            txtPhone.Text = user.Phone;
            txtAddress.Text = user.Address;
            txtEmail.Text = user.Email;
        }

    }
    public string mahoa(string pass)
    {
        return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pass.Trim(), "MD5");
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        var username = Username.Text;
        var pass = txtPassword.Text;
        string passmahoa = mahoa(pass);
        try
        {
            var user = db.Customers
                .Single(c => c.CusId == username && c.Password == passmahoa);
            if (user.Activated == false)
            {
                lblerror.Visible = true;
                lblerror.Text = "Tài khoản của bạn chưa được kích hoạt";
            }
            else
            {
                Session["user"] = user;
                Session["name"] = username;
                Response.Redirect("BookingTour.aspx");
            }
        }
        catch
        {
            lblerror.Visible = true;
            lblerror.Text = "Sai thông tin đăng nhập, vui lòng kiểm tra lại!";
            //Response.Redirect("BookingTour.aspx");
        }

    }
    protected void btn_next_Click(object sender, EventArgs e)
    {
        Session["fullname"] = txtUsername.Text;
        Session["phone"] = txtPhone.Text;
        Session["email"] = txtEmail.Text;
        Session["address"] = txtAddress.Text;
        Session["ngaydi"] = txtNgaydi.Text;
        Session["ghichu"] = notes.Text;
        Response.Redirect("Confirm.aspx");
    }
    protected void ddl_soluong_SelectedIndexChanged(object sender, EventArgs e)
    {
         int TourId = int.Parse(RouteData.Values["TourId"].ToString());
        var tour = db.Tours.Single(p => p.TourId == TourId);
        // kiem tra số lượng khách chọn
        int sl = int.Parse(ddl_soluong.SelectedValue);
        txtTotal.Text =(sl * tour.UnitPrice).ToString();
        Session["sl"] = sl;
        if (int.Parse(ddl_soluong.SelectedValue) == 1)
        {
            Panel2.Visible = Panel3.Visible = Panel4.Visible = Panel5.Visible = Panel6.Visible = Panel7.Visible = Panel8.Visible = Panel9.Visible = Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 2)
        {
            Panel2.Visible = true;
            Panel3.Visible = Panel4.Visible = Panel5.Visible = Panel6.Visible = Panel7.Visible = Panel8.Visible = Panel9.Visible = Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 3)
        {
            Panel3.Visible = Panel2.Visible = true;
            Panel4.Visible = Panel5.Visible = Panel6.Visible = Panel7.Visible = Panel8.Visible = Panel9.Visible = Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 4)
        {
            Panel3.Visible = Panel2.Visible = Panel4.Visible = true;
            Panel5.Visible = Panel6.Visible = Panel7.Visible = Panel8.Visible = Panel9.Visible = Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 5)
        {
            Panel3.Visible = Panel2.Visible = Panel4.Visible = Panel5.Visible = true;
            Panel6.Visible = Panel7.Visible = Panel8.Visible = Panel9.Visible = Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 6)
        {
            Panel3.Visible = Panel2.Visible = Panel4.Visible = Panel5.Visible = Panel6.Visible = true;
            Panel7.Visible = Panel8.Visible = Panel9.Visible = Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 7)
        {
            Panel3.Visible = Panel2.Visible = Panel4.Visible = Panel5.Visible = Panel6.Visible = Panel7.Visible = true;
            Panel8.Visible = Panel9.Visible = Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 8)
        {
            Panel3.Visible = Panel2.Visible = Panel4.Visible = Panel5.Visible = Panel6.Visible =  Panel7.Visible =  Panel8.Visible = true;
            Panel9.Visible = Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 9)
        {
            Panel3.Visible = Panel2.Visible = Panel4.Visible = Panel5.Visible = Panel6.Visible = Panel7.Visible =Panel8.Visible = Panel9.Visible = true;
            Panel10.Visible = false;
        }
        else if (int.Parse(ddl_soluong.SelectedValue) == 10)
        {
            Panel3.Visible = Panel2.Visible = Panel4.Visible = Panel5.Visible = Panel6.Visible = Panel7.Visible =  Panel8.Visible = Panel9.Visible = Panel10.Visible = true;
        }

    }
    protected void ddl_loaikhach_SelectedIndexChanged1(object sender, EventArgs e)
    {
        int TourId = int.Parse(RouteData.Values["TourId"].ToString());
        var tour = db.Tours.Single(p => p.TourId == TourId);

        int loai = int.Parse(ddl_loaikhach.SelectedValue);
        if (loai == 2)
        {
            if (Session["sl"] != null)
            {
                int sl = int.Parse(Session["sl"].ToString());
                int sl1 = sl - 1;
                int tong = int.Parse((sl1 * tour.UnitPrice).ToString());
                tong += int.Parse(tour.PriceChidren.ToString());
                txtTotal.Text = tong.ToString();
            }
        }
        else if (loai == 3)
        {
            if (Session["sl"] != null)
            {
                int sl = int.Parse(Session["sl"].ToString());
                int sl1 = sl - 1;
                int tong = int.Parse((sl1 * tour.UnitPrice).ToString());
                tong += int.Parse(tour.PriceBaby.ToString());
                txtTotal.Text = tong.ToString();
            }
        }
    }
}